package jmine.tec.batch;

import java.util.concurrent.CountDownLatch;

import jmine.tec.batch.JmineTecBatchMessages;
import jmine.tec.batch.PhaseException;
import jmine.tec.batch.domain.ExecPhase;
import jmine.tec.batch.domain.MyConcurrentElement;

/**
 * @author jrenaut
 */
public class MyConcurrentPhase implements ExecPhase<MyConcurrentElement> {

    public CountDownLatch startLatch;

    public CountDownLatch workLatch;

    /**
     * {@inheritDoc}
     */
    public boolean shouldExecute(MyConcurrentElement element) {
        return true;
    }

    /**
     * {@inheritDoc}
     */
    public void execute(MyConcurrentElement element) throws PhaseException {
        this.startLatch.countDown();

        try {
            this.workLatch.await();
        } catch (InterruptedException e) {
            throw new PhaseException(JmineTecBatchMessages.PHASE_GENERIC_PHASE_EXCEPTION.create(), e);
        }
    }

    /**
     * @param startLatch the startLatch to set
     */
    public void setStartLatch(CountDownLatch startLatch) {
        this.startLatch = startLatch;
    }

    /**
     * @param workLatch the workLatch to set
     */
    public void setWorkLatch(CountDownLatch workLatch) {
        this.workLatch = workLatch;
    }

}
